
export default class NodeUtil {

    /*** 寻找子节点，子节点需不重名 */
    public static GetNode(name: string, rootNode: cc.Node): cc.Node {
        if (rootNode.name == name) {
            return rootNode;
        }
        for (let i = 0; i < rootNode.children.length; i++) {
            let node = this.GetNode(name, rootNode.children[i]);
            if (node) {
                return node;
            }
        }

        return null;
    }

    /**找到子节点上的组件*/
    public static GetComponent<T extends cc.Component>(name: string, rootNode: cc.Node, type: { new(): T }): T | null {
        let node = this.GetNode(name, rootNode);
        if (node) {
            const component = node.getComponent(type);
            if (component) {
                return component;
            }
        }

        return null;
    }

    /*** 实例化节点 */
    public static Instantiate(prefab: cc.Prefab, parent: cc.Node, position: cc.Vec2 = cc.Vec2.ZERO) {
        var node = cc.instantiate(prefab);
        node.setParent(parent);
        node.setPosition(position);
        return node;
    }

    public static SetParent(child: cc.Node, parent: cc.Node) {
        child.setParent(parent);
        child.setPosition(cc.Vec3.ZERO);
        child.setRotation(0, 0, 0);
        child.setScale(cc.Vec3.ONE);
    }

    /*** 将点转换成 node 下的局部坐标系 */
    public static SetWorldPosition(node: cc.Node, position: cc.Vec2) {
        if (node.parent) node.setPosition(node.parent.convertToNodeSpaceAR(position));
    }

    public static SetWorldPositionToOtherNode(node1: cc.Node, node2: cc.Node) {
        let position = node2.convertToWorldSpaceAR(cc.Vec2.ZERO);
        if (node1.parent) node1.setPosition(node1.parent.convertToNodeSpaceAR(position));
    }

    /*** 获取关于另一个 node 的局部坐标 */
    public static GetPosition(a: cc.Node, b: cc.Node): cc.Vec2 {
        let worldPosition = this.GetWorldPosition(b);
        return a.convertToNodeSpaceAR(worldPosition);
    }

    /*** 获取 node 的世界坐标 */
    public static GetWorldPosition(node: cc.Node): cc.Vec2 {
        return node.convertToWorldSpaceAR(cc.Vec2.ZERO);
    }

    /*** 获取两个坐标在 XZ 轴的距离 */
    public static GetTwoPosXZDis(aX: number, aZ: number, bX: number, bZ: number) {
        const x = aX - bX;
        const z = aZ - bZ;
        return Math.sqrt(x * x + z * z);
    }

    public static LogVec2(vec2: cc.Vec2, desc: string = "") {
        console.log(`${desc ? `${desc}: ` : ""}x:${vec2.x.toFixed(4)}, y:${vec2.y.toFixed(4)}`);
    }

    public static LogVec3(vec3: cc.Vec3, desc: string = "") {
        console.log(`${desc ? `${desc}: ` : ""}x: ${vec3.x.toFixed(4)}, y:${vec3.y.toFixed(4)}, z:${vec3.z.toFixed(4)} `);
    }

    public static Vec2ToStr(vec2: cc.Vec2, desc: string = "") {
        return `${desc ? `${desc}: ` : ""}[x: ${vec2.x.toFixed(3)}, y:${vec2.y.toFixed(3)}]`;
    }

    public static Vec3ToStr(vec3: cc.Vec3, desc: string = "") {
        return `${desc ? `${desc}: ` : ""}[x: ${vec3.x.toFixed(3)}, y:${vec3.y.toFixed(3)}, z:${vec3.z.toFixed(3)}]`;
    }

}
